
#include "Usb_Control.h"
#include "main.h"
#include <stdio.h>
rt_thread_t tid_ffbwheel;
uint8_t usbSerial[25] = {0};
// 将D+拉低500ms，实现USB Host重新枚举设备的功能
void Usb_Reconnect(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    __HAL_RCC_GPIOA_CLK_ENABLE();

    /*Configure GPIO pins : GPIOA12 USB D+ */
    GPIO_InitStruct.Pin = GPIO_PIN_12;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_PULLDOWN;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET);
    HAL_Delay(500);
}



void FFBWheel_Thread_Entry(void* parameter);
void USBdevice_Thread_Entry(void* parameter)
{

    unsigned int uid = 0;
    //读取usb的序列号
    uid = HAL_GetUIDw2();
    sprintf((char*)usbSerial,"%08x",uid);
    uid = HAL_GetUIDw1();
    sprintf((char*)usbSerial+8,"%08x",uid);
    uid = HAL_GetUIDw0();
    sprintf((char*)usbSerial+16,"%08x",uid);

    MX_USB_OTG_FS_PCD_Init();
    MX_USB_DEVICE_Init();

    //创建并启动ffb wheel进程，主要是处理游戏hid消息
    tid_ffbwheel = rt_thread_create(  "ffbwheel_thread",
                                        FFBWheel_Thread_Entry,
                                        RT_NULL,
                                        FFBW_THREAD_STACK_SIZE,
                                        FFBW_THREAD_PRIORITY,
                                        FFBW_THREAD_TIME_SLICE);
    if(tid_ffbwheel != RT_NULL)
    {
        rt_thread_startup(tid_ffbwheel);
    }

    while(1)
    {
        rt_thread_delay(1000);
    }
}


